package com.example.main

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.app.path.RouterPath
import com.example.common.view.event.CustomEvent
import com.example.common.view.view.BaseMvvmActivity
import com.example.main.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint


@Route(path = RouterPath.PATH_MAIN_PAGE)
@AndroidEntryPoint
class MainActivity :BaseMvvmActivity<ActivityMainBinding>() {
    private val needUserPermission = arrayListOf(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_NUMBERS,
        Manifest.permission.ACCESS_FINE_LOCATION,
    )
    //用户没有同意的权限
    private val unGrantPermission = arrayListOf<String>()

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }
    override fun initView() {
        val hostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        val navHostController = hostFragment.navController
        mDataBinding.bottom.setupWithNavController(navHostController)

    }
    private fun autoRequestPermission():Boolean{
        //判断系统 只有6.0以后才可动态授权
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            //清理一下未授权的用户权限
            unGrantPermission.clear()
            //查找还未授权的权限
            needUserPermission.forEach {
                //检查当前权限是否被用户同意
                var permissionStats = checkSelfPermission(it)
                if(permissionStats == PackageManager.PERMISSION_DENIED){//未授权
                    unGrantPermission.add(it)
                }
            }
            //发起授权
            if(unGrantPermission.size>0){
                requestPermissions(unGrantPermission.toTypedArray(),102)
                return false
            }
        }
        return true
    }

    /**
     * 处理其他业务发送过来的事件
     */
    override fun handEvent(event: CustomEvent) {
        when(event.type){
            100->{
                //设置公告提示

            }
            //有新公告
            101->{

            }
        }
    }
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        var hasUnGranted = false
        if(requestCode == 102 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            unGrantPermission.forEach {
                if(checkSelfPermission(it) == PackageManager.PERMISSION_DENIED){
                    hasUnGranted = true
                }
            }
        }
        if(hasUnGranted){
            AlertDialog.Builder(this).apply {
                setTitle("权限提示")
                setMessage("没有权限,情趣应用详情中设置")
                setPositiveButton("去设置"){ dialog,_->
                    dialog.dismiss()
                    hasUnGranted = false
                    openSettingsAppDetail()
                }
                setNeutralButton("取消"){dialog,_->
                    dialog.dismiss()
                    hasUnGranted = false
                }
            }.show()
        }
    }
    private fun openSettingsAppDetail(){
        val intent = Intent(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
        intent.data = Uri.fromParts("package",packageName,null)
        this.startActivity(intent)
    }
}